<?php

	class Paypal_proxy extends Model
	{
		public function __construct()
		{
			parent::__construct();
		}
		
		
		public function addTempData( $data )
		{
			//Assumes "$data" is a serialized string of an object
			$objstrhash = md5( $data );
						
			$this->db->query('
								INSERT INTO			paypal_temp
													(
														data,
														date_created,
														obj_string_hash
													)
								VALUES				(
														' . $this->db->escape($data) . ',
														Now(),
														' . $this->db->escape($objstrhash) . '	
													)
							 ');
			
			$getPaypalTempId = $this->db->query('
													SELECT 		paypal_temp_id 
													FROM 		paypal_temp
													WHERE		obj_string_hash = ' . $this->db->escape($objstrhash) . '
												  ');

			if( $getPaypalTempId->num_rows() == 1 )
			{
				$paypalTempId = $getPaypalTempId->row()->paypal_temp_id;
			}
			else
				$paypalTempId = 0;

			//Possible for another insert to happen before getting paypalTempId			
			//$paypalTempId = getMaxDbId( 'paypal_temp_id', 'paypal_temp' );
			
			return $paypalTempId;
		}
		
		public function updateTempData( $paypalTempId, $data )
		{
			//Assumes "$data" is a serialized string of an object
			$this->db->query('
								UPDATE paypal_temp
								SET data = ' . $this->db->escape($data) . '
								WHERE paypal_temp_id = ' . $this->db->escape($paypalTempId) . '
							 ');
						
			return $paypalTempId;
		}
		
		public function getTempData( $paypalTempId )
		{
			$getPaypalTempData = $this->db->query('
													SELECT 		data 
													FROM 		paypal_temp
													WHERE		paypal_temp_id = ' . $this->db->escape($paypalTempId) . '
												  ');

			if( $getPaypalTempData->num_rows() == 1 )
			{
				$data = $getPaypalTempData->row()->data;
			}
			
			return $data;
		}
		
		
		public function removeTempData( $paypalTempId )
		{
			$this->db->query('
								DELETE FROM 	paypal_temp
								WHERE			paypal_temp_id = ' . $this->db->escape($paypalTempId) . '
							 ');
			
		}
	}
?>